﻿using AutoMapper;
using DDD.Application;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace GoodsModule.Application.Commands
{
    [AutoMapperTo(typeof(Domain.GoodsAggregate.Goods))]
    public class CreateGoodsCommand : ICommand<GoodsModule.Domain.GoodsAggregate.Goods>
    {
        /// <summary>
        /// 
        /// </summary>
        [Range(1, 999)]
        public int GoodsCategoryId { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        [DisplayName("标题")]
        [Comment("标题")]
        [Required]
        public string Title { get; set; } = string.Empty;
        /// <summary>
        /// 状态
        /// </summary>
        [Comment("状态")]
        [Required]
        [EnumDataType(typeof(Domain.GoodsStatus))]
        public Domain.GoodsStatus Status { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        [Required]
        public uint Sort { get; set; } = 1000;

        /// <summary>
        /// 开始时间
        /// </summary>
        [Comment("开始时间")]
        [Required]
        [DataType(DataType.DateTime)]
        public DateTime StartTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 结束时间
        /// </summary>
        [Comment("结束时间")]
        [Required]
        [DataType(DataType.DateTime)]
        public DateTime EndTime { get; set; } = DateTime.MaxValue;

        /// <summary>
        /// 库存
        /// </summary>
        [Comment("Stock")]
        [Required]
        public int Stock { get; set; } = 0;

        /// <summary>
        /// 经验
        /// </summary>
        [Comment("经验")]
        [Required]
        [Precision(9, 2)]
        public decimal Exp { get; set; } = 0;

        /// <summary>
        /// 积分
        /// </summary>
        [Comment("积分")]
        [Required]
        [Precision(9, 2)]
        public decimal Point { get; set; } = 0;

        /// <summary>
        /// 金额
        /// </summary>
        [Comment("金额")]
        [Required]
        [Precision(9, 2)]
        public decimal Price { get; set; } = 0;

        /// <summary>
        /// 内容
        /// </summary>
        [Comment("内容")]
        [Required]
        public string Content { get; set; } = string.Empty;
    }

    public class CreateGoodsCommandValidator : AbstractValidator<CreateGoodsCommand>
    {
        public CreateGoodsCommandValidator()
        {
            RuleFor(x => x.Title).NotEmpty().MaximumLength(10).WithErrorCode("name error code");
            RuleFor(x => x.Price).InclusiveBetween(18, 60).WithErrorCode("price error code");
        }
    }

    public class CreateGoodsCommandHandler(Infrastructure.Repositories.GoodsRepository repository, IMapper mapper, ILogger<CreateGoodsCommandHandler> logger)
      : ICommandHandler<CreateGoodsCommand, Domain.GoodsAggregate.Goods>
    {
        public async Task<Domain.GoodsAggregate.Goods> Handle(CreateGoodsCommand request, CancellationToken cancellationToken)
        {
            var goods = mapper.Map<Domain.GoodsAggregate.Goods>(request);
            goods = await repository.AddAsync(goods);
            logger.LogInformation("Create Goods {}", goods.Id);
            return goods;
        }
    }
}
